﻿using BaseDal;
using Models;
using System;
using System.Collections.Generic;
using System.Data;

namespace BaseBll
{
    public class UserBll
    {
        static readonly Lazy<UserBll> bll = new Lazy<UserBll>(() => new UserBll());
        public static UserBll Instance { get; } = bll.Value;
        readonly UserDal DaoInstance;
        private UserBll()
        {
            DaoInstance = UserDal.Instance;
        }

        /// <summary>
        /// 登录校验
        /// </summary>
        /// <param name="userAccount">账号</param>
        /// <param name="password">密码</param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public Users CheckAccount(string userAccount, string password, out string msg)
        {
            if (string.IsNullOrEmpty(userAccount))
            {
                msg = "请输入账号！";
                return null;
            }

            if (string.IsNullOrEmpty(password))
            {
                msg = "请输入密码！";
                return null;
            }

            Users model = GetLoginModel(userAccount);

            if (model == null)
            {
                msg = "用户不存在";
                return null;
            }
            msg = "OK";
            return model;
        }

        /// <summary>
        /// 根据账号获取用户
        /// </summary>
        /// <param name="useraccount"></param>
        /// <returns></returns>
        public Users GetLoginModel(string useraccount)
        {
            return DaoInstance.GetLoginModel(useraccount);
        }

        /// <summary>
        /// 数据是否存在
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public bool IsExist(string strWhere)
        {
            return DaoInstance.IsExist(strWhere);
        }

        /// <summary>
        /// 分页查询用户信息
        /// </summary>
        /// <param name="pageSize">当前页</param>
        /// <param name="pageIndex">页尺寸</param>
        /// <param name="strWhere">查询条件</param>
        /// <param name="filedOrder">排序字段</param>
        /// <param name="recordCount">记录数</param>
        /// <returns></returns>
        public DataTable GetPageList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return DaoInstance.GetPageList(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }

        /// <summary>
        /// 根据id查询实体类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Users GetModelById(int id)
        {
            return DaoInstance.GetModelById(id);
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddUser(Users model)
        {
            return DaoInstance.AddUser(model);
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateUser(Users model)
        {
            return DaoInstance.UpdateUser(model);
        }

        /// <summary>
        /// 修改用户登录信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateLogin(Users model)
        {
            return DaoInstance.UpdateLogin(model);
        }

        /// <summary>
        /// 字段校验
        /// </summary>
        /// <param name="model"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool VerifyModel(Users model, ref string msg)
        {

            if (string.IsNullOrEmpty(model.UserName))
            {
                msg = "用户名称不能为空";
                return false;
            }

            string strWhere = "";
            if (model.ID > 0)
            {
                strWhere = " and ID !=" + model.ID;

            }

            if (IsExist(" and UserAccount='" + model.UserAccount + "'" + strWhere))
            {
                msg = "用户账号已存在";
                return false;
            }

            return true;
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteUser(List<string> id, string userName)
        {
            return DaoInstance.DeleteUser(id, userName);
        }

        /// <summary>
        /// 修改状态（启用、禁用）
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public int UpdateStatus(int id, int status, string userName)
        {
            return DaoInstance.UpdateStatus(id, status, userName);
        }

        /// <summary>
        /// 用户认证
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int AuditUser(string id)
        {
            return DaoInstance.AuditUser(id);
        }

    }
}
